package com.cdtu.controller;

import com.cdtu.entity.SysEmployee;
import com.cdtu.entity.SysMenu;
import com.cdtu.service.SysEmployeeService;
import com.cdtu.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2023/8/8 18:16
 * @Description : TODO 用来接收...的请求
 */
@Controller
@RequestMapping("/user")
//解决跨域问题
@CrossOrigin
public class LoginController extends BaseController {

    @Autowired
    private SysEmployeeService sysEmployeeService;

    @Autowired
    private SysMenuService sysMenuService;

    @RequestMapping("/login")
    public Object login(SysEmployee employee, ModelAndView modelAndView, HttpServletRequest request, HttpServletResponse response) {
        String password = employee.getPassword();
        SysEmployee sysEmployeeDB = sysEmployeeService.getEmployeeByUsername(employee.getUsername());
        //--》判断
        if (sysEmployeeDB != null) {
            if (password.equals(sysEmployeeDB.getPassword())) {
                //友情提示
                modelAndView.addObject("message", "登录成功");
                //############################## 取出登录用户自己权限 ##########################################
                //1.登录用户信息保存Session
                HttpSession session = request.getSession();
                session.setAttribute("USER", sysEmployeeDB);
                //2.取出登录用户自己权
                Long empId = sysEmployeeDB.getId();

                // 实例化HashMap
                HashMap<Long, List<SysMenu>> menuLvMap = new HashMap<>();


                // b、查询登录用户拥有的权限
                //----->一级目录
                List<SysMenu> firstLvMenuList = sysMenuService.queryLoginUserMyPMenuByEmpId(empId);
                for (SysMenu sysMenu : firstLvMenuList) {
                    //---》一級目錄下二级目录（自己的儿子自己带）
                    //找关系
                    Long menuId = sysMenu.getMenuId();
                    List<SysMenu> permissionSelectedListSon = sysMenuService.queryMenuSonByM_id(menuId);
                    //找到自己的子节点之后还要查看自己是否有子节点的权限

                    permissionSelectedListSon = permissionSelectedListSon.stream().filter(item->{
                        return firstLvMenuList.contains(item);
                    }).collect(Collectors.toList());

                    // 维护父节点和子节点关系
                    menuLvMap.put(menuId, permissionSelectedListSon);
                }
                //返回数据
                modelAndView.addObject("firstLvMenuList", firstLvMenuList);
                modelAndView.addObject("menuLvMap", menuLvMap);
                //############################## 取出登录用户自己权限 ##########################################
                //2.跳转页面;
                modelAndView.setViewName("system/main");
                return modelAndView;
            } else {
                //友情提示
                modelAndView.addObject("message", "用户名或密码错误，请重新输入！");
                //2.跳转页面;
                modelAndView.setViewName("redirect:index.jsp");
                return  modelAndView;

            }
        } else {
            //友情提示
            modelAndView.addObject("message", "用户名或密码错误，请重新输入！");
            //2.跳转页面;
            modelAndView.setViewName("redirect:/index.jsp");
            return  modelAndView;
        }
    }
}
